﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InformationSystem.Framework.Dao;
using Spring.Data.Generic;
using InformationSystem.BLL.Core.Model;
using System.Data;
using Spring.Data.Common;

namespace InformationSystem.BLL.Core.Dao.Impl
{
    public class SharedDocumentDaoImpl : InfoSysAdoDaoSupport, ISharedDocumentDao
    {


        public bool CreateDocument(Model.FileInformation finfo)
        {
            string insertSql = @"insert into Documents(uploadfilename,savefilename,createuserid,createusername,collegeid,gradeid,ispublictocollege,createtime) 
                                 values 
                                (@uploadfilename, @savefilename, @createuserid, @createusername, @collegeid,@gradeid, @ispublictocollege, @createtime)";
            IDbParametersBuilder builder = CreateDbParametersBuilder();
            builder.Create().Name("uploadfilename").Type(DbType.String).Value(finfo.UploadFileName);
            builder.Create().Name("savefilename").Type(DbType.String).Value(finfo.SaveFileName);
            builder.Create().Name("createuserid").Type(DbType.String).Value(finfo.CreateUserID);
            builder.Create().Name("createusername").Type(DbType.String).Value(finfo.CreateUserName);
            builder.Create().Name("collegeid").Type(DbType.Int32).Value(finfo.CollegeId);
            builder.Create().Name("gradeid").Type(DbType.Int32).Value(finfo.GradeId);
            builder.Create().Name("ispublictocollege").Type(DbType.Boolean).Value(finfo.IsPublicToCollege);
            builder.Create().Name("createtime").Type(DbType.DateTime).Value(finfo.CreateTime);
            return AdoTemplate.ExecuteNonQuery(CommandType.Text, insertSql, builder.GetParameters()) == 1;

        }

        public bool DeleteDocument(int fileid, string userid)
        {
            string deleteSql = @"delete from Documents where autoid=@fileid and createuserid=@createuserid";
            IDbParametersBuilder builder = CreateDbParametersBuilder();
            builder.Create().Name("fileid").Type(DbType.Int32).Value(fileid);
            builder.Create().Name("createuserid").Type(DbType.String).Value(userid);
            return AdoTemplate.ExecuteNonQuery(CommandType.Text, deleteSql, builder.GetParameters()) == 1;

        }

        public bool DeleteDocument(int fileid)
        {
            string deleteSql = @"delete from Documents where autoid=@fileid";
            IDbParametersBuilder builder = CreateDbParametersBuilder();
            builder.Create().Name("fileid").Type(DbType.Int32).Value(fileid);
            return AdoTemplate.ExecuteNonQuery(CommandType.Text, deleteSql, builder.GetParameters()) == 1;
        }

        public IList<Model.FileInformation> GetDocuments(string userid)
        {
            string selectSql = @"select
	                                autoid,uploadfilename,savefilename,createuserid,
	                                createusername,collegeid,gradeid,ispublictocollege,createtime
                                from
	                                Documents
                                where
	                                createuserid=@createuserid";
            IDbParametersBuilder builder = CreateDbParametersBuilder();
            builder.Create().Name("createuserid").Type(DbType.String).Value(userid);
            return AdoTemplate.QueryWithRowMapper<FileInformation>(CommandType.Text, selectSql, new UserRow<FileInformation>(), builder.GetParameters());
        }

        public IList<Model.FileInformation> GetDocuments(int collegeid, int gradeid)
        {
            string selectSql = @"select
	                                autoid,uploadfilename,savefilename,createuserid,
	                                createusername,collegeid,gradeid,ispublictocollege,createtime
                                from
	                                Documents
                                where
	                                collegeid=@collegeid and gradeid=@gradeid";
            IDbParametersBuilder builder = CreateDbParametersBuilder();
            builder.Create().Name("collegeid").Type(DbType.Int32).Value(collegeid);
            builder.Create().Name("gradeid").Type(DbType.Int32).Value(gradeid);
            return AdoTemplate.QueryWithRowMapper<FileInformation>(CommandType.Text, selectSql, new UserRow<FileInformation>(), builder.GetParameters());
        }

        public Model.FileInformation GetDocumentDetailByID(int fileid)
        {
            string selectSql = @"select
	                                autoid,uploadfilename,savefilename,createuserid,
	                                createusername,collegeid,gradeid,ispublictocollege,createtime
                                from
	                                Documents
                                where
	                                autoid=@fileid";
            IDbParametersBuilder builder = CreateDbParametersBuilder();
            builder.Create().Name("fileid").Type(DbType.Int32).Value(fileid);
            try
            {
                return AdoTemplate.QueryForObject<FileInformation>(CommandType.Text, selectSql, new UserRow<FileInformation>(), builder.GetParameters());
            }
            catch (Exception)
            { 
            
            }
            return null;
            
        }

        internal class UserRow<T> : IRowMapper<T> where T : FileInformation, new()
        {
            public T MapRow(IDataReader reader, int rowNum)
            {
                T fileInformation = new T();

                fileInformation.FileID = reader.GetInt32(reader.GetOrdinal("autoid"));
                fileInformation.UploadFileName = reader.GetString(reader.GetOrdinal("uploadfilename"));
                fileInformation.SaveFileName = reader.GetString(reader.GetOrdinal("savefilename"));
                fileInformation.CreateUserID = reader.GetString(reader.GetOrdinal("createuserid"));
                fileInformation.CreateUserName = reader.GetString(reader.GetOrdinal("createusername"));
                fileInformation.CollegeId = reader.GetInt32(reader.GetOrdinal("collegeid"));
                fileInformation.GradeId = reader.GetInt32(reader.GetOrdinal("gradeid"));
                fileInformation.IsPublicToCollege = reader.GetBoolean(reader.GetOrdinal("ispublictocollege"));
                fileInformation.CreateTime = reader.GetDateTime(reader.GetOrdinal("createtime"));

                return fileInformation;
            }
        }


        public IList<FileInformation> GetDocuments(LoginUser user, int recordcount)
        {
            string selectSql = @"select
	                                autoid,uploadfilename,savefilename,createuserid,
	                                createusername,collegeid,gradeid,ispublictocollege,createtime
                                from
	                                Documents
                                where
	                                (collegeid=@collegeid and gradeid=@gradeid) or (collegeid=@collegeid and ispublictocollege=1) or (collegeid=0 and gradeid=0)";
            IDbParametersBuilder builder = CreateDbParametersBuilder();
            builder.Create().Name("collegeid").Type(DbType.Int32).Value(user.CollegeID);
            builder.Create().Name("gradeid").Type(DbType.Int32).Value(user.GradeID);
            return AdoTemplate.QueryWithRowMapper<FileInformation>(CommandType.Text, string.Format(selectSql, recordcount == 0 ? "" : ("top " + recordcount.ToString())), new UserRow<FileInformation>(), builder.GetParameters());

        }
    }
}
